define([ './module' ], function(goodsspecialmodule) {
	'use strict';


	goodsspecialmodule.controller('goodsspecialcontent',goodsspecialcontentctrl);
	function goodsspecialcontentctrl($stateParams,$http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer,Upload,$timeout,GoodsTypeService,goodscategoryService) {

		var vm = this;
		vm.message = '';
		vm.someClickHandler = someClickHandler;
		vm.selected = {};
		vm.selectAll = false;
		vm.toggleAll = toggleAll;
		vm.toggleOne = toggleOne;
		vm.dtInstance = {};
		var titleHtml = '<input type="checkbox" ng-model="gt.selectAll" ng-click="gt.toggleAll(gt.selectAll,gt.selected)">';
		vm.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
				type: 'POST',
				url: '../mall/products/findProductByPage',
				dataSrc: 'data'
			})
			.withOption('processing', true)
			.withOption('paging', true)
			.withOption('serverSide', true)
			.withOption('createdRow', function (row, data, dataIndex) {
				$compile(angular.element(row).contents())($scope);
			})
			.withOption('headerCallback', function (header) {
				if (!$scope.headerCompiled) {
					$scope.headerCompiled = true;
					$compile(angular.element(header).contents())($scope);
				}
			})
			.withOption('stateSave', true)
			.withOption('rowCallback', rowCallback)
			.withPaginationType('full_numbers')
			.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest', function () {

		});
		vm.dtColumns = [
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function (data, type, full, meta) {
				vm.selected[full.id] = false;
				return "<input type='checkbox' ng-model=\"gt.selected['" + data.id + "']\" ng-click=\"gt.toggleOne(gt.selected)\">";
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn(null).withTitle('图片').notSortable().renderWith(imgHtml),
			DTColumnBuilder.newColumn('psn').withTitle('货号').notSortable(),
			DTColumnBuilder.newColumn('productname').withTitle('货物名称').notSortable(),
			DTColumnBuilder.newColumn('nname').withTitle('分类').notSortable(),
			DTColumnBuilder.newColumn('goodstypename').withTitle('商品类型').notSortable(),
			DTColumnBuilder.newColumn('stock').withTitle('库存').notSortable(),
			DTColumnBuilder.newColumn('saleprice').withTitle('销售价').notSortable(),
			DTColumnBuilder.newColumn('memberprice').withTitle('会员价').notSortable(),
			DTColumnBuilder.newColumn('salestatus').withTitle('销售状态').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable()];
		function imgHtml(data, type, full, meta) {
			return '<img style="width:50px;height:50px;" ng-src="' + data.mainimg + '" alt="Photo" />';
		}

		function reloadData() {
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}

		function callback(data) {
		}


		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData() {
			var resetPaging = false;
			var searchParams = {
				isCustomSearch: 1,
				name: '',
				psn: '',
				goodstypeid: ''
			};
			if ($scope.productname != undefined) {
				searchParams.name = $scope.productname;

			}
			if ($scope.psn != undefined) {
				searchParams.psn = $scope.psn;
			}
			if ($scope.selectedgoodstype.goodstypeid != undefined) {
				searchParams.goodstypeid = $scope.selectedgoodstype.goodstypeid;
			} else {
				$scope.errors = {
					show: true,
					msg: '请选择商品类型'
				}
				return false;
			}
			vm.dtOptions['ajax'].data = searchParams;
			vm.dtInstance.reloadData(callback, resetPaging);
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom = function () {
			searchReloadData();
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll, selectedItems) {
			for (var id in selectedItems) {
				if (selectedItems.hasOwnProperty(id)) {
					selectedItems[id] = selectAll;
				}
			}
		}

		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems) {
			var me = this;
			for (var id in selectedItems) {
				if (selectedItems.hasOwnProperty(id)) {
					if (!selectedItems[id]) {
						me.selectAll = false;
					}
				}
			}
			me.selectAll = true;

		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
			console.log("======" + info);
			$scope.pushToSelectedProducts(info.id, info);
		}

		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function () {
				$scope.$apply(function () {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}


		//选定的专题货物

		$scope.pushToSelectedProducts = function (index, product) {
			var tag=true;
			angular.forEach($scope.sp,function(d){
				if(d.id==product.id){
					alert("该商品已经存在");
					tag=false;
				}
			});
			if(tag){
				$scope.sp.push(product);
			}
			$scope.selectedproducts=$scope.sp;
		};


		/**
		 * 删除选择的货物
		 * @param index
		 */
		$scope.removeselectproduct = function (index) {
			$scope.selectedproducts.splice(index, 1);
			$scope.sp.slice(index,1);
		}


		//查询所有商品类型
		$scope.findallgoodstype = function () {
			var promise = GoodsTypeService.query();
			promise.then(function (data) {
				$scope.goodstypeselect = data.beanlist;
			}, function (data) {
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		};


		/**
		 * 显示货物搜索区域
		 */
		$scope.showsearchproduct = function () {
			$scope.searchproduct = true;
		}


		var operate = $location.search().operate || $stateParams.operate;

		if (operate != undefined && operate == 'edit') {
			//控制保存按钮显示
			$scope.updatebtn = {show: true};
			//设置启用 1表示启用
			$scope.status = "1";
			$scope.title="正在编辑专题内容";
			var id = $location.search().id;
			if (id != undefined && id != "") {
				$http({
					method: 'POST',
					url: '../mall/goodsspecialcontent/find',
					data: {
						'goodsSpecialTId': id
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.title = '正在对专题--' + data.bean.title + '--的内容进行编辑';
						//填充选择的货物
						$scope.sp=[];
						$scope.selectedproducts = data.beanlist;
						angular.forEach(data.beanlist,function(d){
							$scope.sp.push(d);
						});

						$scope.updatebtn = {show: true};
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}

			$scope.update = function () {
				$http({
					method: 'POST',
					url: '../mall/goodsspecialcontent/update',
					data: {
						'goodsSpecialTId': id,
						'content': angular.toJson($scope.selectedproducts),
						'status': $scope.status
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.info = {
							show: true,
							msg: '商品专题内容更新成功'
						}
						$location.path('/goodsspecialment');
					} else {
						$scope.errors = {
							show: true,
							msg: data.msg
						}
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}

		}
	}

});
